<?php
Database($_SESSION['cid']);
require_once 'Admin/Extend/Vendor/oss/sdk.class.php';

class InformationAction extends Action{

    public function index(){
        $_GET['order'] == '' && $_GET['order'] = 'information_id desc';
        $rs=pageing('information',10,'',$_GET['seach'],$_GET['order'],'time_insert>time_delete and category='.$_GET['category'],$col='title');
        $rs['channel']=D('Information_channel')->get_all();
        $this->assign("rs",$rs);
        $this->display();
    }

    public function upt_img(){
        $this->upload_information();
        $data['information_image']=$_POST['information_image'];
        $rs=M('image_control_center','','DB_MEETING')->find();
        if($rs)
            $n=M('image_control_center','','DB_MEETING')->where('id='.$rs['id'])->save($_POST);
        else
            $n=M('image_control_center','','DB_MEETING')->add($_POST);
        if($n)
            $this->success('修改成功');
        else
            $this->error('修改失败');
    }
    //增
    public function addAct(){
        if($_POST['url_type']==2){
            $_POST['url_editor']=$_POST['url_upload'];
        }
        if($_POST['url_editor']==''){$this->error('上传文件不存在!!');}
        $arr=get_headers($_POST['url_editor'],true);
        $_POST['size']=round($arr['Content-Length']/1048576,2);
        $_POST['type']=strtolower(end(explode('.',$_POST['url_editor'])));
        $_POST['url_type']=1;

        if($_POST['category']==4 && $_FILES['information_image']['name']!=''){
            $this->upload_information();
        }
        $rs = D('Public/Add')->add_data('information');
        if($rs)
            $this->success('添加成功');
        else
            $this->error('添加失败');
    }
    //删
    public function delAct(){
        //删除阿里云数据
        $rs=M('information','','DB_MEETING')->where('id='.$_GET['id'])->find();
        $obj = new ALIOSS();
        //设置是否打开curl调试模式
        $obj->set_debug_mode(FALSE);
        $bucket = 'zh-'.C('DOMAIN_TAG_NAME');
        $object = str_replace('http://oss.aliyuncs.com/zh-'.C('DOMAIN_TAG_NAME').'/','',$rs['url_editor']);
        $response = $obj->delete_object($bucket,$object);

        $rs  = D('Public/Del')->del_data('information');
        if ($rs){
            echo '{"state":"0","msg":"delete success"}';
        } else {
            echo '{"state":"-1","msg":"delete false"}';
        }
    }
    //改
    public function to_upt(){
        $rs = D('Information')->get_information($_GET['id']);
        switch($rs['category']){
            case 1:$rs['sections']=array('V系专区','沙系专区','E系专区','其他');break;
            case 2:$rs['sections']=array('人力资源','行政IT','财务');break;
        }
        $rs['channel']=D('Information_channel')->get_all();
        $this->assign('rs',$rs);
        $this->display();
    }
    public function uptAct(){

        if($_POST['url_type']==2){
            $_POST['url_editor']=$_POST['url_upload'];
        }
        if($_POST['url_editor']==''){
            $this->error('上传文件不存在!!');
        }
        $_POST['url_type']=1;
        $arr=get_headers($_POST['url_editor'],true);
        $_POST['size']=round($arr['Content-Length']/1048576,2);
        $_POST['type']=strtolower(end(explode('.',$_POST['url_editor'])));
        $_POST['url_type']=1;
        /*else{
            $rs=M('information','','DB_MEETING')->where('id='.$_POST['id'])->find();
            //文件有更新
            if($_POST['url_editor']!=$rs['url_editor']){
                $arr=get_headers($_POST['url_editor'],true);
                $_POST['size']=round($arr['Content-Length']/1048576,2);
                $_POST['type']=strtolower(end(explode('.',$_POST['url_editor'])));
                $_POST['url_type']=1;
                //删除阿里云数据
                $obj = new ALIOSS();
                //设置是否打开curl调试模式
                $obj->set_debug_mode(FALSE);
                $bucket = 'zh-'.C('DOMAIN_TAG_NAME');
                $object = str_replace('http://oss.aliyuncs.com/zh-teamin/','',$rs['url_editor']);
                $response = $obj->delete_object($bucket,$object);
            }
        }*/

        if($_POST['category']==4 && $_FILES['information_image']['name']!=''){
            $this->upload_information();
        }
        $rs = D('Public/Upt')->upt_data('information');
        if($rs){
            $this->success('修改成功');
        }else
            $this->error('修改失败');
    }

    public function upload_information(){
        //创建文件夹
        $dataDir="Public/Uploads/information/";
        if(!is_dir($dataDir)){mkdir($dataDir,0777,true);}
        $dataDir="Public/Uploads/information/".getLoginPid().'/';
        if(!is_dir($dataDir)){mkdir($dataDir,0777,true);}
        //文件上传
        import("ORG.Net.UploadFile");
        $config['savePath'] = $dataDir;
        $upload = new UploadFile($config);
        if(!$upload->upload()){
            $this->error($upload->getErrorMsg());
        }else{
            $uploadList = $upload->getUploadFileInfo();
            foreach($uploadList as $val){
                if($val['key']=='url_upload'){
                    /*$_POST['url_upload']=$dataDir.$val['savename'];
                    $_POST['size']=round($_FILES['url_upload']['size']/1048576,2);
                    $_POST['type']=end(explode('.',$_POST['url_upload']));
                    $_POST['savename']=$val['savename'];*/
                }else{
                    $_POST['information_image']=$dataDir.$val['savename'];
                    $_POST['savename']=$val['savename'];
                }
            }
        }
    }

    public function check_suggest(){
        if($_GET['order']=='')$_GET['order']='time_insert desc';
        $_GET['information_category']=$_GET['information_category']==''?'1':$_GET['information_category'];
        $rs=pageing('information_suggest',10,'',$_GET['seach'],$_GET['order'],'information_category='.$_GET['information_category'],$col='suggest_content',$now='check_suggest');
        foreach($rs['data'] as $val){
            $val['time_insert']=date("Y-m-d H:i",$val['time_insert']);
            $val['suggest_state']=$val['suggest_state']=='1'?'显示':'隐藏';
            $val['suggest_resolve']=$val['suggest_resolve']=='1'?'已解决':'未处理';
            $arr[]= $val;
        }
        $now_start=strtotime(date("Y-m-d")." 00:00:00");
        $now_end=strtotime(date("Y-m-d")." 23:59:59");
        $rs['new_count']=M('information_suggest','','DB_MEETING')->where('time_insert>= '.$now_start.' and time_insert<='.$now_end)->count();
        $rs['data']=$arr;
        $this->assign("rs",$rs);
        $this->display();
    }

    public function suggest_display(){
        $rs=M('information_suggest','','DB_MEETING')->where('suggest_id='.$_GET['suggest_id'])->save(array('suggest_state'=>$_GET['new_display']));
        if($rs)
            echo '{"state":0}';
        else
            echo M('information_suggest','','DB_MEETING')->getLastSql();
    }
    public function suggest_resolve(){
        $rs=M('information_suggest','','DB_MEETING')->where('suggest_id='.$_GET['suggest_id'])->save(array('suggest_resolve'=>$_GET['suggest_resolve']));
        if($rs)
            echo '{"state":0}';
        else
            echo '{"state":-1}';
    }
    public function delete_suggest(){
        $rs=M('information_suggest','','DB_MEETING')->where('suggest_id='.$_GET['suggest_id'])->delete();
        if($rs)
            echo '{"state":0}';
        else
            echo '{"state":-1}';
    }
}
?>